package com.biz.primus.ms.commodity.dao.po;

import com.biz.primus.base.enums.ExtendType;
import com.biz.primus.commodity.vo.ExtendOptionVo;
import com.biz.primus.common.utils.JsonUtils;
import com.biz.primus.ms.base.jpa.po.BaseEntity;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;

@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name = "pro_product_extend_new")
public class Extend extends BaseEntity implements Serializable{

    /**
     * 扩展属性唯一标识
     */
    @Column(name = "extend_key",length = 20,unique = true,nullable = false)
    private String key;

    @Column(length = 20,nullable = false)
    private String label;

    /**
     * 分类ID
     */
    @Column
    private Long categoryId;

    @Column(nullable = false)
    private Integer idx;

    /**
     * 属性值  jsonArray
     * @see ExtendOptionVo
     */
    @Column(name = "extend_option",columnDefinition = "TEXT")
    private String option;

    @Enumerated(EnumType.STRING)
    @Column(length = 32,nullable = false)
    private ExtendType extendType;

    public List<ExtendOptionVo> getOptions(){
        if (StringUtils.isNotBlank(option)){
            return JsonUtils.json2Obj(option, List.class, ExtendOptionVo.class);
        }
        return Collections.emptyList();
    }
}
